package com.lock.pojo;

import com.lock.enums.ExceptionEnum;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

/**
 * http返回数据
 *
 * @author lcf
 */
@Data
@NoArgsConstructor
public class HttpResult<T> {

    private Integer code;

    private String msg;

    private T data;

    public HttpResult(T data) {
        this.code = ExceptionEnum.SUCCESS.getCode();
        this.msg = "";
        this.data = data;
    }

    public HttpResult(T data, String msg) {
        this.code = ExceptionEnum.SUCCESS.getCode();
        this.msg = msg;
        this.data = data;
    }

    public static HttpResult<String> success(String msg) {
        return new HttpResult<>(ExceptionEnum.SUCCESS.getCode(), msg);
    }

    public static HttpResult<String> success() {
        return new HttpResult<>(ExceptionEnum.SUCCESS.getCode(), ExceptionEnum.SUCCESS.getMsg());
    }

    public static HttpResult<String> error(String msg) {
        return new HttpResult<>(ExceptionEnum.ERROR.getCode(), msg);
    }

    public static HttpResult<String> error() {
        return new HttpResult<>(ExceptionEnum.ERROR.getCode(), ExceptionEnum.ERROR.getMsg());
    }

    public HttpResult(Exception e) {
        this.code = ExceptionEnum.ERROR.getCode();
        this.msg = StringUtils.isBlank(e.getMessage()) ? "系统异常" : e.getMessage();
    }

    public HttpResult(ExceptionEnum e) {
        this.code = e.getCode();
        this.msg = e.getMsg();
    }

    /**
     * 私有构造函数
     */
    private HttpResult(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }
}
